<?php

class Article extends AppModel
{

	public $name = 'Article';

	public $displayField = 'name';

	protected $allowed_tags = array(
		'HELP', /*'NEWS', 'NEW',*/
	);

	public $validate = array(
        'title' => array(
            'create_first' => array(
                'on' => 'create',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'required',
            ),
            'update_first' => array(
                'on' => 'update',
                'required' => false,
                'allowEmpty' => false,
                'message' => 'required',
            ),
        ),
        'url_title' => array(
            'create_first' => array(
                'on' => 'create',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'required',
            ),
            'update_first' => array(
                'on' => 'update',
                'required' => false,
                'allowEmpty' => false,
                'message' => 'required',
            ),
            'second2' => array(
                'rule' => array('allowUrlTilte'),
                'message' => 'must_be_unique',
                'allowEmpty' => true,
            ),
        ),
        'content' => array(
            'create_first' => array(
                'on' => 'create',
                'required' => true,
                'allowEmpty' => false,
                'message' => 'required',
            ),
            'update_first' => array(
                'on' => 'update',
                'required' => false,
                'allowEmpty' => false,
                'message' => 'required',
            ),
            'second2' => array(
                'rule' => array('allowContent'),
                'message' => 'Article.content.dont_parse',
                'allowEmpty' => true,
            ),
        ),
	);

	function allowTag($data)
	{
	    $tags = explode(',', $data);
	    return !(bool)count(array_diff($tags, array_values($this->permissions)));
	}

	function getTags()
	{
		$count = 0;

		$result = array();
		foreach ($this->allowed_tags as $tag) {
			if (++$count > 64) {
				break;
			}
			$result[pow(2, ($count - 1))] = $tag;
		}
	    return $result;
	}

	function getTagsList()
	{
		$count = 0;

		$result = array();
		foreach ($this->allowed_tags as $tag) {
			if (++$count > 64) {
				break;
			}
			$result[pow(2, ($count - 1))] = __d('articles', 'tag_'.$tag, true);
		}
	    return $result;
	}

	function allowDelete($fields = null)
	{
        return true;
	}

	function allowUpdate($fields = null)
	{
        return true;
	}

	function allowUrlTilte($data)
	{
        return ! is_array($this->find(array("`{$this->primaryKey}`" => "!= ".intval($this->id), "`url_title`" => $data)));
	}

	function allowContent($data)
	{
        return true;
	}

	function beforeSave()
	{
	    $creating = ! $this->exists();
	    if ($creating) {
            $this->data[$this->name]['created'] = date('Y-m-d H:i:s');
            if (!isset($this->data[$this->name]['tags'])) {
                $this->data[$this->name]['tags'] = 'HELP';
                $this->data[$this->name]['created'] = date('Y-m-d H:i:s');
            }
	    }
	    $changed_content = true;
	    if (! $creating) {
            $id = $this->id ? $this->id : @intval($this->data[$this->name][$this->primaryKey]);
            $this->rel();
            $existed_content = $this->field('content', array($this->primaryKey => $id));
            $changed_content = $existed_content != $this->data[$this->name]['content'];
	    }
	    if ($changed_content) {
	        try {
                $this->data[$this->name]['cache'] =& $this->renderContent($this->data[$this->name]['content']);
	        } catch (Exception $e) {
	            $this->validationErrors['content'] = __d('errors', 'Article.content.dont_parse', true);
	            return false;
	        }
	    }
	    return parent::beforeSave();
	}

	function &renderContent($content)
	{
	    $result = $content;
	    return $result;
	}
}

?>